package com.bootssm.controller;

import com.bootssm.pojo.*;
import com.bootssm.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;

    @PostMapping("/findAll")
    public PageView<Category> findAll(@RequestBody CategoryVO categoryVO){
        Integer page=categoryVO.getPage();
        Integer start=(page-1)*categoryVO.getSize();
        categoryVO.setStart(start);
        List<Category> cates = categoryService.findAll(categoryVO);
        Integer count =categoryService.getCount(categoryVO);
        PageView<Category> pageView=new PageView<>();
        pageView.setDataList(cates);
        pageView.setCount(count);
        return pageView;
    }


    @PostMapping("/insert")
    public Result insert(@RequestBody Category category){
        try{
            categoryService.insert(category);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","插入失败");
        }
        return new Result("200","插入成功");
    }

    @GetMapping("/findById/{categoryId}")
    public Category findById(@PathVariable("categoryId") String categoryId){
        return categoryService.findById(categoryId);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Category category){
        try{
            categoryService.update(category);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","修改失败");
        }
        return new Result("200","修改成功");
    }

    @GetMapping("/delete/{categoryId}")
    public Result delete(@PathVariable("categoryId") String categoryId){
        try{
            categoryService.delete(categoryId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result("500","删除失败");
        }
        return new Result("200","删除成功");
    }

    @PostMapping ("/select")
    public List<Category> select(){
        List<Category> categoryList=categoryService.select();
        return categoryList;
    }
}
